/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao.concreto;

import bean.Consulta;
import bean.Usuario;
import interfaces.IDAOConsulta;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.management.InstanceNotFoundException;
import javax.sql.rowset.CachedRowSet;

/**
 *
 * @author personal
 */
public class DaoConslulta implements IDAOConsulta{

    protected ResourceBundle rb;

    public DaoConslulta() {
        rb = ResourceBundle.getBundle("sql.sqlConsulta");
    }

 
    public static boolean existe(CachedRowSet consultaApp, Consulta oConsulta) {
        try {

            consultaApp.setObject(1, oConsulta.getIdPaciente().getIdUsuario());
            consultaApp.setObject(2, oConsulta.getIdEspecialista().getIdUsuario());
            consultaApp.setObject(3, oConsulta.getNroEvaluacion());
            consultaApp.execute();
            consultaApp.beforeFirst();

            if (consultaApp.next()) {
                return true;
            } else {
                return false;
            }
        } catch (SQLException ex) {
            Logger.getLogger(DaoConslulta.class.getName()).log(Level.SEVERE, null, ex);
        }
        return true;
    }

   

    public Consulta findByPacienteEval(Connection conn, String paciente, String evaluacion, String especialidad) throws InstanceNotFoundException {
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            int i = 0;
            ps = conn.prepareStatement(rb.getString("SQL_FIND_BY_PACIENTE_EVAL"));
            ps.setString(++i, paciente);
            ps.setString(++i, evaluacion);
            ps.setString(++i, especialidad);
            rs = ps.executeQuery();
            while (rs.next()) {
                return (crearObjeto(rs));
            }
            return null;
        } catch (SQLException e) {
            e.printStackTrace();

        } finally {
            try {
                rs.close();
                ps.close();
            } catch (SQLException ex) {
                Logger.getLogger(Consulta.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return null;
    }

    private Consulta crearObjeto(ResultSet rs) throws SQLException {
        Consulta oConsulta = new Consulta();
        Usuario oUsuarioEspecialista = new Usuario();
        oUsuarioEspecialista.setIdUsuario(rs.getString("especialista"));
        oUsuarioEspecialista.setApellido1(rs.getString("apellido1"));
        oUsuarioEspecialista.setApellido2(rs.getString("apellido2"));
        oUsuarioEspecialista.setNombres(rs.getString("nombres"));
        oConsulta.setObservacion(rs.getString("observacion"));
        oConsulta.setFecha(rs.getDate("fecha"));
        oConsulta.setIdEspecialista(oUsuarioEspecialista);
        return oConsulta;
    }
    
        public String insertarConsulta(Connection conn, Consulta oConsulta) {
        String sRetorno = "";
        CallableStatement cllStatement = null;
        try {
            cllStatement = conn.prepareCall("{ ? = call insertar_consulta(?,?)}");
            cllStatement.setString(1, oConsulta.getIdPull());
            cllStatement.setString(2, oConsulta.getIdEvaluacionPaciente());
/*            cllStatement.setDate(3, (Date) oConsulta.getFecha());
            cllStatement.setBigDecimal(4, oConsulta.getNroEvaluacion());
            cllStatement.setString(5, oConsulta.getObservacion());
 *
 */
            cllStatement.registerOutParameter(3, Types.VARCHAR);
            cllStatement.executeUpdate();
            sRetorno = cllStatement.getString(3);

            return sRetorno;
        } catch (NumberFormatException ex) {
            ex.printStackTrace();
        } catch (SQLException ex) {
            ex.printStackTrace();
        } finally {
            try {
                cllStatement.close();
            } catch (SQLException ex) {
                Logger.getLogger(dao.concreto.DaoConslulta.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return sRetorno;
    }
        
    public Consulta obtenerConsulta(Connection conn, String idPull, String idEvaluacionPaciente) {
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            int i = 0;
            ps = conn.prepareStatement(rb.getString("SQL_OBTENER_CONSULTA"));
            ps.setString(++i, idPull);
            ps.setString(++i, idEvaluacionPaciente);
            rs = ps.executeQuery();
            while (rs.next()) {
                Consulta retorno = new Consulta();
                retorno.setIdConsulta(rs.getString("idconsulta"));
                return retorno;
            }

        } catch (SQLException e) {
            e.printStackTrace();

        } finally {
            try {
                rs.close();
                ps.close();
            } catch (SQLException ex) {
                Logger.getLogger(Consulta.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return null;
    }        
}
